package com.csun.cmny.provider.service.impl;

import com.csun.cmny.base.dto.LoginAuthDto;
import com.csun.cmny.provider.dao.OldmanNurseMapper;
import com.csun.cmny.provider.pojo.*;
import com.csun.cmny.provider.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class OldmanNurseServiceImpl implements OldmanNurseService {

    @Resource
    private OldmanNurseMapper oldmanNurseMapper;
    @Autowired
    private OldmanCheckInService oldmanCheckInService;
    @Autowired
    private OldmanFeeService oldmanFeeService;
    @Autowired
    private OldmanLeaveService oldmanLeaveService;

    @Override
    public int insert(OldmanNurse nurse) {
        return oldmanNurseMapper.insert(nurse);
    }

    @Override
    public List<OldmanNurse> selectByOldmanId(Map<String,Object> queryMap) {
        return oldmanNurseMapper.selectByOldmanId(queryMap);
    }

    @Override
    public List<OldmanNurse> selectByOldmanIdOrderByNurseDate(Map<String, Object> queryMap) {
        return oldmanNurseMapper.selectByOldmanIdOrderByNurseDate(queryMap);
    }

    @Override
    public OldmanNurse selectByOldmanIdAndDate(Integer oldmanId, String currentDate) {
        return oldmanNurseMapper.selectByOldmanIdAndDate(oldmanId,currentDate);
    }

    @Override
    public int updateByBillId(OldmanNurse nurse) {
        return oldmanNurseMapper.updateByPrimaryKeySelective(nurse);
    }
    private int updateAllByBillId(OldmanNurse nurse) {
        return oldmanNurseMapper.updateByPrimaryKey(nurse);
    }
    @Override
    public int delOldNurseByOid(Integer oldmanId) {
        return oldmanNurseMapper.delOldNurseByOid(oldmanId);
    }

    @Override
    public OldmanNurse selectRecentNurse(Integer oldmanId) {
        return oldmanNurseMapper.selectRecentNurse(oldmanId);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveOrUpdateFee(OldmanNurse nurse,LoginAuthDto loginAuthDto){
        //根据老人id判断最近是否有记录，有就更新没有就插入
        OldmanFee oldmanFee = oldmanFeeService.selectByOldmanId(nurse.getOldmanId());
        if(oldmanFee==null){
            //存消费表
            OldmanFee fee = new OldmanFee();
            fee.setCheckinoldmanId(nurse.getOldmanId());
            OldmanCheckIn oldmanCheckIn = oldmanCheckInService.selectById(nurse.getOldmanId());
            if (oldmanCheckIn!=null && oldmanCheckIn.getActPaidFee()!=null){
                //预缴金额
                fee.setActPaidFee(oldmanCheckIn.getActPaidFee());
                /**
                 * 余额
                 * 初始余额 = 预缴金额-押金
                 * 首次护理：余额=初始余额-护理费用
                 * 押金不算做余额，出院结算时再返还押金
                 */
                fee.setRemainder(oldmanCheckIn.getActPaidFee().subtract(oldmanCheckIn.getDeposit()).subtract(nurse.getTotalFee()));
            }
            //消费总金额
            fee.setMonetary(nurse.getTotalFee());
            fee.setModifier(loginAuthDto.getUserId());
            //插入费用表
            oldmanFeeService.insert(fee);
            OldmanLeave oldmanLeave = new OldmanLeave();
            oldmanLeave.setSettleId(oldmanFeeService.selectByOldmanId(nurse.getOldmanId()).getSettleId());
            oldmanLeave.setOldmanId(nurse.getOldmanId());
            oldmanLeave.setModifier(loginAuthDto.getUserId());
            oldmanLeave.setModifyTime(new Date());
            oldmanLeaveService.updateByOid(oldmanLeave);
        }else{
            oldmanFee.setMonetary(oldmanFee.getMonetary().add(nurse.getTotalFee()));
            oldmanFee.setRemainder(oldmanFee.getRemainder().subtract(nurse.getTotalFee()));
            oldmanFeeService.updateByPrimaryKeySelective(oldmanFee);
            OldmanLeave oldmanLeave = new OldmanLeave();
            oldmanLeave.setSettleId(oldmanFee.getSettleId());
            oldmanLeave.setOldmanId(nurse.getOldmanId());
            oldmanLeave.setModifier(loginAuthDto.getUserId());
            oldmanLeave.setModifyTime(new Date());
            oldmanLeaveService.updateByOid(oldmanLeave);
        }
    }

}
